package com.hqyj.shop.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.shop.dao.ShowGoodsDao;
import com.hqyj.shop.pojo.ShowGoods;
import com.hqyj.shop.pojo.PageData;
import com.hqyj.shop.service.ShowGoodsSerice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
//import sun.security.provider.SHA;

import java.util.List;
@Service
public class ShowGoodsServiceImpl implements ShowGoodsSerice {
    @Autowired
    private ShowGoodsDao showGoodsDao;
    @Override
    public List<ShowGoods> queryAll() {
        List<ShowGoods> goods= showGoodsDao.queryAll();
        return goods;
    }

    @Override
    public List<ShowGoods> queryByName(String name) {
        List<ShowGoods> goods= showGoodsDao.queryByName(name);
        return goods;
    }

    @Override
    public List<ShowGoods> queryByCategory(String category) {
        List<ShowGoods> goods= showGoodsDao.queryByCategory(category);
        return goods;
    }


    @Override
    public String getImageUrl(ShowGoods showGoods) {
        return showGoods.getpImage();
    }

    @Override
    public ShowGoods queryById(int id) {
        return showGoodsDao.queryById(id);
    }

    @Override
    public List<ShowGoods> queryPutOn() {
        List<ShowGoods> goods= showGoodsDao.queryPutOn();
        return goods;
    }

    @Override
    public PageData<ShowGoods> queryByPage(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        List<ShowGoods> showGoodsList = showGoodsDao.queryAll();
        PageInfo<ShowGoods> pageInfo = new PageInfo<>(showGoodsList);
        PageData<ShowGoods> pageData = new PageData<>();
        pageData.setCurrentPage(pageNumber);
        pageData.setPageSize(pageSize);
        pageData.setTotalPage(pageInfo.getPages());
        pageData.setTotalSize((int)pageInfo.getTotal());
        if (pageInfo.isHasNextPage()){
            //又下一页,配置下一页的页码
            pageData.setNextPage(pageInfo.getNextPage());
        }else {
            //没有下一页,就是尾页
            pageData.setNextPage(pageInfo.getPages());
        }
        if (pageInfo.isHasPreviousPage()){
            //有上一页,设置上一页的页码
            pageData.setPrevuousPage(pageInfo.getPrePage());
        }else {
            //没有上一页
            pageData.setPrevuousPage(1);
        }
        pageData.setList(pageInfo.getList());
        return pageData;

    }

    @Override
    public List<ShowGoods> queryBySearch(String search) {

        List<ShowGoods> list= showGoodsDao.queryByName(search);
        if (list == null){
            return null;
        }
        return list;
    }

    @Override
    public List<ShowGoods> queryByMoneyMore(double money) {
        List<ShowGoods> goods =showGoodsDao.queryByMoneyMore(money);
        return goods;
    }

    @Override
    public List<ShowGoods> queryByMoneyLess(double money) {
        List<ShowGoods> goods =showGoodsDao.queryByMoneyLess(money);
        return goods;
    }

    @Override
    public PageData<ShowGoods> getGoodsByPage(int pageNumber, int pageSize,PageInfo<ShowGoods> pageInfo) {
        PageData<ShowGoods> pageData = new PageData<>();
        pageData.setCurrentPage(pageNumber);
        pageData.setPageSize(pageSize);
        pageData.setTotalPage((int)pageInfo.getTotal());
        if (pageInfo.isHasNextPage()){
            //又下一页,配置下一页的页码
            pageData.setNextPage(pageInfo.getNextPage());
        }else {
            //没有下一页,就是尾页
            pageData.setNextPage(pageInfo.getPages());
        }
        if (pageInfo.isHasPreviousPage()){
            //有上一页,设置上一页的页码
            pageData.setPrevuousPage(pageInfo.getPrePage());
        }else {
            //没有上一页
            pageData.setPrevuousPage(1);
        }
        pageData.setList(pageInfo.getList());
        return pageData;
    }

    @Override
    public int addadd(int id, String add) {
        showGoodsDao.addadd(id,add);
        return 1;
    }
}
